package com.xiaoxu.intranetweb.mybatisplusSzappdb.controller.truck;

import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.alibaba.fastjson.JSONObject;
import java.net.URLEncoder;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.business.truck.TruckClassBu;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.dao.UserInfoDao;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.excelEntity.TruckClassExcelEn;
import com.xiaoxu.intranetweb.util.PermissionCheck;
import com.xiaoxu.intranetweb.util.SzResult;
import com.xiaoxu.intranetweb.util.WebFunctionUtil;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;

/**
 * 品种查询控制器
 *
 * @author xiaoxu
 * @version 1.0
 * @date 2022/1/5 15:00
 * @since 1.0
 */
@CrossOrigin()
@RestController
@RequestMapping(value = "/truckClass")
public class TruckClassController {
    @Autowired
    private TruckClassBu TruckClassBu;
    @Autowired
    private UserInfoDao userInfoDao;
    @Autowired
    private WebFunctionUtil webFunctionUtil;
    /**
     * 品种类型查询接口
     *
     * @return SzResult
     */
    @GetMapping("/getTruckClass")
    public SzResult getTruckClass(HttpServletRequest request) throws Exception {
        String plantId = request.getHeader("Plant-Id");
        return TruckClassBu.getTruckClass(plantId);
    }

    //新增品种类型
    @PostMapping("/addTruckClass")
    @PermissionCheck("truckClass_addTruckClass")
    public SzResult addTruckClassClass(@RequestBody JSONObject jsonObject, HttpServletRequest request) throws Exception {
        String truckClass = jsonObject.getString("truckClass");//品种类型
        String remark = jsonObject.getString("remark");//备注
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        return TruckClassBu.addTruckClass(truckClass, remark, token, plantId);
    }

    //修改品种类型
    @PostMapping("/updateTruckClass")
    @PermissionCheck("truckClass_updateTruckClass")
    public SzResult updateTruckClassClass(@RequestBody JSONObject jsonObject, HttpServletRequest request) throws Exception {
        String id = jsonObject.getString("id");//品种类型id
        String truckClass = jsonObject.getString("truckClass");//品种类型
        String remark = jsonObject.getString("remark");//备注
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        return TruckClassBu.updateTruckClass(id, truckClass, remark, token, plantId);
    }

    //删除品种类型
    @GetMapping("/deleteTruckClass")
    @PermissionCheck("truckClass_deleteTruckClass")
    public SzResult deleteTruckClassClass(@RequestParam String id, HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
        return TruckClassBu.deleteTruckClass(id, token, plantId);
    }


    /**
     * 车辆类型导表
     */

    @PostMapping("/exportExcel")
    public void exportExcel(@RequestBody JSONObject jsonParam, HttpServletResponse response, HttpServletRequest request) throws Exception {
        String token = request.getHeader("Authorization");
        String plantId = request.getHeader("Plant-Id");
//        String plantId = null;
//        if (token !=null) {
//            plantId = userInfoDao.getUserFactory(StpUtil.getLoginIdByToken(token).toString()).getId();
//        }
        //        实体类
        List<Map<String, Object>> fields = jsonParam.getObject("fields", List.class);
//        文件名使用utf-8编码
        String filename = jsonParam.getString("filename");
//        数据
        List<Map<String, Object>> ids = jsonParam.getObject("ids", List.class);
//        是否有表头
        Boolean isHeader = jsonParam.getBoolean("isHeader");
//        mode为"current"时，导出当前页，为"all"时，导出所有页，selected时，导出选中数据
        String mode = jsonParam.getString("mode");
//        是否原始数据
        Boolean original = jsonParam.getBoolean("original");
//        分页
        Map<String, String> pager = jsonParam.getObject("pager", Map.class);
//        表格标题
        String sheetName = jsonParam.getString("sheetName");
        Map<String, String> form = jsonParam.getObject("form", Map.class);

        String truckclassId = null;
        if (form.get("truckclassId") != null && !form.get("truckclassId").equals("null")) {
            truckclassId = form.get("truckclassId");
        }
        String truckclassClass = null;
        if (form.get("truckclassClass") != null && !form.get("truckclassClass").equals("null")) {
            truckclassClass = form.get("truckclassClass");
        }

        String truckclassRemark = null;
        if (form.get("truckclassRemark") != null && !form.get("truckclassRemark").equals("null")) {
            truckclassRemark = form.get("truckclassRemark");
        }
        Future<List<TruckClassExcelEn>> listFuture = TruckClassBu.exportExcel(fields, ids, filename, isHeader, mode, original, pager, form, sheetName, response, plantId, truckclassId, truckclassClass, truckclassRemark);

        List<TruckClassExcelEn> truckClassExcelEn = listFuture.get();
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        BorderStyle borderStyle = BorderStyle.THIN;
        short borderColor = IndexedColors.BLACK.getIndex();
        contentWriteCellStyle.setBorderTop(borderStyle);
        contentWriteCellStyle.setBorderBottom(borderStyle);
        contentWriteCellStyle.setBorderLeft(borderStyle);
        contentWriteCellStyle.setBorderRight(borderStyle);
        contentWriteCellStyle.setTopBorderColor(borderColor);
        contentWriteCellStyle.setBottomBorderColor(borderColor);
        contentWriteCellStyle.setLeftBorderColor(borderColor);
        contentWriteCellStyle.setRightBorderColor(borderColor);
        HorizontalCellStyleStrategy horizontalCellStyleStrategy =
                new HorizontalCellStyleStrategy(null, contentWriteCellStyle);
        response.setContentType("application/vnd.ms-excel; charset=utf-8");
        response.setCharacterEncoding("utf-8");
        if (filename == null || filename.equals("")) {
            filename = "车辆类型";
        }
        response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8") + ".xlsx");
        EasyExcel.write(response.getOutputStream(), TruckClassExcelEn.class).registerWriteHandler(horizontalCellStyleStrategy).sheet("车辆类型").doWrite(truckClassExcelEn);
        response.getOutputStream().flush();
        response.getOutputStream().close();
    }

}